package com.cn.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.cn.domain.User;
import com.cn.utils.CurrentUserUtils;

/**
 * Servlet Filter implementation class CurrentUserFilter
 */
public class CurrentUserFilter implements Filter {

    /**
     * Default constructor. 
     */
    public CurrentUserFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		User u = (User) req.getSession().getAttribute("user");
		System.out.println(u);
		CurrentUserUtils.bind(u);
		chain.doFilter(request, response);
		CurrentUserUtils.remove();
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
